package com.zj.aopspringboot01;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.sql.Timestamp;
import java.util.Arrays;

@Component //将切面类声明为一个bean
@Aspect //声明这是一个切面类
public class LogAnnotationAspect {
    //注意这里的注解是要写全名
    @Pointcut("@annotation(org.springframework.web.bind.annotation.PostMapping)")
    public void pointcut1(){}
    @Pointcut("@annotation(org.springframework.web.bind.annotation.GetMapping)")
    public void pointcut2(){}

    @Around("pointcut1()||pointcut2()")
    public Object logAdd(ProceedingJoinPoint pj) {
        long now = System.currentTimeMillis();

        //前置通知部
        System.out.println("start: "+now+" function:" + pj.getSignature()+" args:"+ Arrays.toString(pj.getArgs()));
        Object proceed = null;
        try {
            proceed = pj.proceed();

        } catch (Throwable e) {
            //异常通知部分
            System.out.println("exception = " + e.getMessage());
        }finally {
            //后置通知也叫最终通知部分
            System.out.println("finally cost: " + (System.currentTimeMillis()-now));
        }
        return  proceed;
    }


}
